/* 
 * Copyright (C) 2011 rccloud.com All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.rccloud.inproxy;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.rccloud.inproxy.annotation.AutoProxy;
import com.rccloud.inproxy.utils.PackageScanner;

/**
 * Config loader, it used to load config info.
 */
public class InproxyConfigImpl implements InproxyConfig {

	/**
	 * Default config file
	 */
	public static final String DEFAULT_CONFIG_FILE = "/inproxy.properties";

	private static final String SPLIT = ",";
	private static final String BASE_PACKAGE = "inproxy.package";
	private static final String INCLUDE = "inproxy.include";
	private static final String EXCLUDE = "inproxy.exclude";

	private Properties properties;

	public InproxyConfigImpl() throws IOException {
		this(DEFAULT_CONFIG_FILE);
	}

	public InproxyConfigImpl(String propertyFile) throws IOException {
		this(InproxyConfigImpl.class.getResourceAsStream(propertyFile));
	}

	public InproxyConfigImpl(InputStream inputStream) throws IOException {
		properties = new Properties();
		properties.load(inputStream);
	}

	@Override
	public List<Class<?>> getAnnotatedClasses() {
		List<String> classes = new ArrayList<String>();

		// Handle package scan
		String packages = properties.getProperty(BASE_PACKAGE);
		if (packages != null) {
			String[] packageNames = packages.split(SPLIT);
			classes = PackageScanner.scanClasses(packageNames);
		}

		// Add include classes
		String include = properties.getProperty(INCLUDE);
		if (include != null) {
			String[] includes = include.split(SPLIT);
			for (String className : includes) {
				if (!classes.contains(className)) {
					classes.add(className);
				}
			}
		}

		// Remove exclude classes
		String exclude = properties.getProperty(EXCLUDE);
		if (exclude != null) {
			String[] excludes = exclude.split(SPLIT);
			for (String className : excludes) {
				if (classes.contains(className)) {
					classes.remove(className);
				}
			}
		}

		// Find annotated classes
		List<Class<?>> result = new ArrayList<Class<?>>();
		for (String className : classes) {
			try {
				Class<?> clazz = Class.forName(className, false, getClass()
						.getClassLoader());
				if (clazz.isAnnotationPresent(AutoProxy.class)) {
					result.add(clazz);
				}
			} catch (Exception e) {
				continue;
			}
		}
		return result;
	}
}
